<?php
/**
 * Allos to select working language pair
 * 
 * @author Joseph Chereshnovsky <joseph.chereshnovsky@gmail.com>
 */
class Translation_PairController extends Wise_Controller_Abstract
{
	/**
	 * Shows the language pair selection form
	 * and process language pair update
	 * 
	 * @return void
	 */
	public function indexAction()
	{
		$req = $this->getRequest();
		
		if ($req->isPost()) {

			$data = $req->getPost();

			if (!empty($data['source_lang']) && !empty($data['target_lang'])) {

				// Get the list of available languages for selectboxes
				$availLanguages = $this['Languages']->fetchAll();
				// build languages title index
				$titleIndex = array();
				foreach ($availLanguages as $lang) {
					$titleIndex[$lang->id] = $lang->langI18n;
					$codeIndex[$lang->id]  = $lang->iso_639_1_code;
				}
				
				$this->User->source_lang		= $data['source_lang'];
				$this->User->source_lang_title	= $titleIndex[$data['source_lang']];
				$this->User->source_lang_code	= $codeIndex[$data['source_lang']];
								
				$this->User->target_lang		= $data['target_lang'];
				$this->User->target_lang_title	= $titleIndex[$data['target_lang']];
				$this->User->target_lang_code	= $codeIndex[$data['target_lang']];
				
				// check if user searching some phrase right now
				$search_phrase = $this->User->search_phrase; 
				
				$this->_redirect('/translation/search' . (!empty($search_phrase) ? '/' . urlencode($search_phrase) : '' ) );
			}
		}
		
		// Prepare the list of available languages for selectboxes
		$this->view->availLanguages = $this['Languages']->fetchAll();
	}

}